package com.airlines.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.airlines.crewassignment.domain.DateTimeRange;

/**
 * @author xxxx
 * @date 2018年4月12日 
 * @description
 */
 
public class WeekUtil {
	private static final Logger logger = LoggerFactory.getLogger(WeekUtil.class);

	/**
	 * 根据日期返回该日期属于周几
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date){
		DateTime dateTime = new DateTime(date);
		return dateTime.getDayOfWeek();
	}
	
	/**
	 * 判断一个周几，是否在一个周范围内
	 * @param weeks
	 * @param week
	 * @return
	 */
	public static  boolean weekCheck(String weeks,int week){
		boolean flag = false;
		char[] weekArray = weeks.toCharArray();
		for(char w :weekArray){
			if(StringUtils.equals(String.valueOf(week), String.valueOf(w))){
				flag = true;
			}
		}
		
		return flag;
	}
	
	/**
	 * 判断一个周几，是否在一个周范围内
	 * @param weekList
	 * @param week
	 * @return
	 */
	public static boolean weekCheck(List<Integer> weekList,int week){
		for(Integer w : weekList){
			if(w == week){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 根据班期，位移几天，判定位移的班期是多少
	 * @param weeks
	 * @param dayDiff
	 * @return
	 */
	public static String getWeeksNext(String weeks,int dayDiff){
		char[] weekAll = {'1','2','3','4','5','6','7'};
		char[] weekChars = weeks.toCharArray();
		
		StringBuffer sb = new StringBuffer();
		for(int i = 0 ; i < weekChars.length ; i++){
			char week = weekChars[i];
			//计算是星期几
			int weekInt = getIndex(weekAll,week);
			
			int weekIntNext = (weekInt + dayDiff)%7;
			if(weekIntNext  == 0 ){
				weekIntNext  = 7 ; 
			}
			
			sb.append(weekIntNext);
		}
		
		return sb.toString();
		
		
	}
	
	private static int getIndex(char[] weekAll, char week){
		for(int i = 0 ; i < weekAll.length ; i++){
			char c = weekAll[i];
			if(c == week){
				return i+1;
			}
		}
		
		return -1;
	}
	
	/**
	 * 根据日期和班期，返回日期列表
	 * @param dateTimeRange
	 * @param weeks
	 * @return
	 */
	public static List<Date> getDateList(DateTimeRange dateTimeRange , String weeks){
		DateTime start = new DateTime(dateTimeRange.getStart()).withTimeAtStartOfDay();
		DateTime end = new DateTime(dateTimeRange.getEnd()).millisOfDay().withMaximumValue();
		List<Date> list = new ArrayList<>();
		for(DateTime temp = start; !temp.isAfter(end) ;temp = temp.plusDays(1)){
			int week = getWeek(temp.toDate());
			if(weekCheck(weeks, week)){
				list.add(temp.toDate());
			}
		}
		return list;
	}
	
	/**
	 * 判断两个日期班期之前是否有交集
	 * @param dateTimeRange1
	 * @param weeks1
	 * @param dateTimeRange2
	 * @param weeks2
	 * @return
	 */
	public static boolean isIntersection(DateTimeRange dateTimeRange1 , String weeks1,DateTimeRange dateTimeRange2,String weeks2){
		List<Date> list1 = getDateList(dateTimeRange1, weeks1);
		List<Date> list2 = getDateList(dateTimeRange2, weeks2);
		Set<Date> set = new HashSet<>();
		set.addAll(list1);
		set.addAll(list2);
		if(set.size() < list1.size() + list2.size()){
			return true;
		}else{
			return false;
		}
	}
	
	
	public static void main(String[] args) {
		String weeks = getWeeksNext("7246", 1);
		logger.debug(weeks+"");
	}
	
}
